﻿using IterationLife.ContentResources.Models;
using IterationLife.ContentResources.Queries;
using IterationLife.ContentResources.Repositories;
using IterationLife.Datas;
using IterationLife.PlatformBackEnd.AppServices.Contracts.ContentResources;
using IterationLife.PlatformBackEnd.AppServices.Dtos.ContentResources;
using Util.Applications;
using Util.Datas.Queries;
using Util.Domains.Trees;

namespace IterationLife.PlatformBackEnd.AppServices.Impl.ContentResources {
    /// <summary>
    /// 内容资源分类服务
    /// </summary>
    public class ContentResourceCategoryService : TreeServiceBase<ContentResourceCategory, ContentResourceCategoryDto, ContentResourceCategoryQuery>, IContentResourceCategoryService {
        /// <summary>
        /// 初始化内容资源分类服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="contentResourceCategoryRepository">内容资源分类仓储</param>
        public ContentResourceCategoryService( IApplicationUnitOfWork unitOfWork, IContentResourceCategoryRepository contentResourceCategoryRepository )
            : base( unitOfWork, contentResourceCategoryRepository ) {
            ContentResourceCategoryRepository = contentResourceCategoryRepository;
        }
        
        /// <summary>
        /// 内容资源分类仓储
        /// </summary>
        protected IContentResourceCategoryRepository ContentResourceCategoryRepository { get; set; }
        
        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override ContentResourceCategoryDto ToDto( ContentResourceCategory entity ) {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override ContentResourceCategory ToEntity( ContentResourceCategoryDto dto ) {
            return dto.ToEntity();
        }
        
        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">内容资源分类查询实体</param>
        protected override IQueryBase<ContentResourceCategory> CreateQuery( ContentResourceCategoryQuery param ) {
            return new Query<ContentResourceCategory>( param )
                .Filter( new TreeCriteria<ContentResourceCategory>( param ) )
                .Filter(t => t.Code.Contains(param.Code))
                .Filter(t => t.Name.Contains(param.Name));
        }
    }
}